package com.tanhua.sso.controller;

import cn.hutool.core.convert.Convert;
import com.tanhua.sso.service.MyCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class MyCenterController {

    @Autowired
    private MyCenterService myCenterService;

    @Autowired
    private UserInfoController userInfoController;

    @PostMapping("/phone/sendVerificationCode")
    public Object sendVerificationCode(@RequestHeader("Authorization")String token){
        return   this.myCenterService.sendVerificationCode(token);

    }


    @PostMapping("/phone/checkVerificationCode")
    public Object checkVerificationCode(@RequestHeader("Authorization")String token, @RequestBody Map<String,Object> param){
        String code = Convert.toStr(param.get("verificationCode"));
        Boolean result = this.myCenterService.checkVerificationCode(token, code);
        HashMap<String, Object> map = new HashMap<>();
        map.put("verification", result);
        return map;
    }

    @PostMapping("/phone")
    public Object updatePhone(@RequestHeader("Authorization")String token, @RequestBody Map<String,Object> param){
          String phone = Convert.toStr(param.get("phone"));
          return   this.myCenterService.updatePhone(token,phone);
    }

    /**
     * 上传头像
     *
     * @param file
     * @param token
     * @return
     */
    @PostMapping("header")
    public Object saveLogo(@RequestParam("headPhoto") MultipartFile file, @RequestHeader("Authorization") String token) {
        return this.userInfoController.loginRegInfoLogo(file, token);
    }
}
